#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient
import sys


def parseCSV(line):
    line = line.strip()
    if line == "":
        return None
    else:
        splitline = []
        while line != "":
            if line.startswith("\""):
                line = line[1:]
                index = line.find("\"")
                item = line[0:index]
                splitline.append(item)
                line = line[index + 1:]
            else:
                index = line.find(",")
                if index == -1:
                    splitline.append(line)
                    line = ""
                else:
                    item = line[0:index]
                    splitline.append(item)
                    line = line[index:]
            line = line.lstrip()
            if line.startswith(","):
                line = line[1:]
        return splitline


def addPMC():
    SERVER = 'localhost'
    PORT = 27017
    DB_NAME = "journal"
    COLLECTION_NAME = "journal"

    connection = MongoClient(SERVER, PORT)
    db = connection[DB_NAME]
    collection = db[COLLECTION_NAME]

    infile = open(sys.argv[1], "r")
    for line in infile:
        line = line.rstrip()
        if line != "":
            addtoset = {}
            splitline = parseCSV(line)
            FullTitle = splitline[0]
            MedAbbr = splitline[1]
            Publisher = splitline[4]
            NlmId = splitline[5]
            URL = splitline[12]
            if FullTitle != "":
                addtoset["FullTitle"] = FullTitle
            if MedAbbr != "":
                addtoset["MedAbbr"] = MedAbbr
            if Publisher != "":
                addtoset["Publisher"] = Publisher
            if URL != "":
                addtoset["URL"] = URL
            collection.update_one({"NlmId": NlmId}, {"$addToSet": addtoset})
    connection.close()
    infile.close()


def addPMCmiss():
    SERVER = 'localhost'
    PORT = 27017
    DB_NAME = "journal"
    COLLECTION_NAME = "journal"

    connection = MongoClient(SERVER, PORT)
    db = connection[DB_NAME]
    collection = db[COLLECTION_NAME]

    infile = open(sys.argv[2], "r")
    for line in infile:
        line = line.rstrip()
        if line != "":
            set = {}
            splitline = parseCSV(line)
            FullTitle = splitline[0]
            MedAbbr = splitline[1]
            pISSN = splitline[2]
            eISSN = splitline[3]
            Publisher = splitline[4]
            NlmId = splitline[5]
            URL = splitline[12]
            if FullTitle != "":
                set["FullTitle"] = [FullTitle]
            if MedAbbr != "":
                set["MedAbbr"] = [MedAbbr]
            if pISSN != "":
                set["pISSN"] = pISSN
            if eISSN != "":
                set["eISSN"] = eISSN
            if Publisher != "":
                set["Publisher"] = [Publisher]
            if NlmId != "":
                set["NlmId"] = NlmId
            if URL != "":
                set["URL"] = [URL]
            collection.insert_one(set)
    connection.close()
    infile.close()


if __name__ == "__main__":
    # addPMC()
    addPMCmiss()
